/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        MOSS Faceted Search
/// Author:         Leonid Lyublinski (leonidly@microsoft.com)
/// Company:        Microsoft Services
/// Date:           05/04/2007  Version:        1.0
///
/// ===========================================================================

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Microsoft.SharePoint.Portal.ExtendedSearch.Common
{
    /// <summary>
    /// The class facilitates synchronizations between un-connected web parts on the page
    /// </summary>
    internal sealed class WPManager
    {       

        private static WebPart FindWebPartByType(Page page, string wpType)
        {
            WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(page);
            if (wpm == null) return null; // probably we part preview
            WebPartCollection wpc = wpm.WebParts;
            foreach (WebPart wp in wpc)
            {
                if (wp.GetType().FullName == wpType) return wp;
            }
            return null;
        }

        public static WebPartDisplayMode DisplayMode(Page page)
        {
            WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(page);
            if (wpm == null) return null;
            return wpm.DisplayMode;
        }

        /// <summary>
        /// Returns reference to the 1st CoreResultsWebPart on the page
        /// </summary>
        /// <param name="page">web page</param>
        /// <returns><typeparamref name="Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart"/></returns>
        public static Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart CoreResultsWebPart(Page page)
        {
            WebPart wp = FindWebPartByType(page, "Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart");
            if (wp == null) return null;
            Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart crwp = (Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart) wp;
            return crwp;
        }
        
        /// <summary>
        /// Returns reference to the 1st PeopleCoreResultsWebPart on the page
        /// </summary>
        /// <param name="page">web page</param>
        /// <returns><typeparamref name="Microsoft.Office.Server.Search.WebControls.PeopleCoreResultsWebPart"/></returns>
        public static Microsoft.Office.Server.Search.WebControls.PeopleCoreResultsWebPart PeopleCoreResultsWebPart(Page page)
        {
            WebPart wp = FindWebPartByType(page, "Microsoft.Office.Server.Search.WebControls.PeopleCoreResultsWebPart");
            if (wp == null) return null;
            Microsoft.Office.Server.Search.WebControls.PeopleCoreResultsWebPart pcrwp = (Microsoft.Office.Server.Search.WebControls.PeopleCoreResultsWebPart)wp;
            return pcrwp;
        }

        /// <summary>
        /// Returns reference to the 1st SearchBox on the page
        /// </summary>
        /// <param name="page">web page</param>
        /// <returns><typeparamref name="Microsoft.Office.Server.Search.WebControls.SearchBox"/></returns>
        public static Microsoft.SharePoint.Portal.WebControls.SearchBox SearchBoxWebPart(Page page)
        {
            WebPart wp = FindWebPartByType(page, "Microsoft.SharePoint.Portal.WebControls.SearchBox");
            if (wp == null) return null;
            Microsoft.SharePoint.Portal.WebControls.SearchBox crwp = (Microsoft.SharePoint.Portal.WebControls.SearchBox)wp;
            return crwp;
        }
    }
}